<?php

namespace App\Service;

use App\Library\Common;
use App\Model\ShopSendAddress;
use Hyperf\Snowflake\IdGeneratorInterface;
use Hyperf\Utils\ApplicationContext;
use Psr\Container\ContainerInterface;
use Hyperf\Di\Annotation\Inject;

//use function Symfony\Component\Translation\t;

class ShopSendAddressService extends BaseService
{
    /**
     * @Inject
     * @var AreainfoService
     */
    private $areainfo;

    /**
     *  * 查询列表
     * @param array $params
     * @param int $shop_id
     * @return object
     */
    public function index(array $params, $shop_id = 1)
    {
        $where = [];
        $fields = ['id', 'link_name', 'is_shop_addr', 'phone', 'created_at', 'updated_at', 'address_info', 'street_id', 'street_str', 'shop_id', 'create_by', 'update_by'];
        return $this->getModelInstance()
            ->where('shop_id', $shop_id)
            ->with(['create_info', 'update_info', 'delete_info', 'shop_info'])
            ->orderBy('is_shop_addr', 'DESC')
            ->orderBy('id', 'DESC')
            ->select($fields)->get();
    }


    /**
     * 查询默认发货地址
     * @param int $shop_id
     * @return object
     */
    public function get_default_addr($shop_id = 1)
    {
        $fields = ['id', 'link_name', 'is_shop_addr', 'phone', 'created_at', 'updated_at', 'address_info', 'street_id', 'street_str', 'shop_id', 'create_by', 'update_by'];
        return $this->getModelInstance()
            ->where('shop_id', $shop_id)
            ->where('is_shop_addr', 1)
            ->with(['create_info', 'update_info', 'delete_info', 'shop_info'])
            ->first($fields);
    }

    /**
     * @param int $shop_id
     * @param int $addr_id
     * @return array
     */
    public function getAddrById(int $shop_id, int $addr_id): array
    {
        $addr = $this->getModelInstance()
            ->where('shop_id', '=', $shop_id)
            ->where('id', '=', $addr_id)
            ->first();
        return empty($addr) ? [] : $addr->toArray();
    }


    /**
     * @param array $insertData
     * @return int
     */
    public function address_insert(array $insertData)
    {
        return $this->insertAndGetId(Common::filter_null($insertData));
    }

    /**
     * @param array $upData
     * @return bool
     */
    public function update_address(array $upData)
    {
        return ShopSendAddress::query()->where('id', $upData['id'])->update($upData);
    }

    /**
     * @param int $id
     * @param int $shop_id
     * @return bool
     */
    public function info(int $id=0,$shop_id=0)
    {
        if($id==0&&$shop_id!=0){
            $info = $this->getModelInstance()
                ->with(['create_info', 'update_info', 'delete_info', 'shop_info'])
                ->where('shop_id',$shop_id)
                ->where('is_shop_addr',1)
                ->first();
        }else{
            $info = $this->getModelInstance()
                ->with(['create_info', 'update_info', 'delete_info', 'shop_info'])
                ->find($id);
        }

        if ($info) {
            $info = $info->toarray();
            $info['address_arr'] = $this->areainfo->getFullAddress($info['street_id']);
        }
        return $info;
    }

}